package io.renren.modules.app.controller;

import com.qcloud.cos.model.PutObjectResult;
import io.renren.modules.app.entity.VideoListEntity;
import io.renren.modules.app.service.VideoListService;
import io.renren.modules.app.utils.CosConstant;
import io.renren.modules.app.utils.CosUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@RestController
@Api
@RequestMapping({"app/file"})
public class UploadController
{

  @Autowired
  private VideoListService videoListService;

  @PostMapping({"/upload"})
  @ApiOperation("上传文件")
  public String SingleFileUpLoad(@RequestParam("file") MultipartFile file)
  {
    if (file.isEmpty()) {
      return "文件为空";
    }
    //文件的大小，
    long fileSize = file.getSize();
    System.out.println("fileSize: " + fileSize);
    String contentType = file.getContentType();
    System.out.println("contentType: " + contentType);

    InputStream inputStream = null;
    OutputStream outputStream = null;
    String fileName = "";
    String fileShortName = "";
    PutObjectResult putObjectResult = null;

    //存档路径  linux的话估计要改一改
    String path = "d:/upload/";

    try
    {
      inputStream = file.getInputStream();
      //这个应该是带后缀的跟File.getName()应该是一样的
      fileName = file.getOriginalFilename();
      System.out.println("fileName:"+fileName);
      //获取不带后缀的名字
      fileShortName = fileName.substring(0, fileName.lastIndexOf("."));
      System.out.println("fileShortName:" + fileShortName);

      File targetFile = new File(path + fileName);
      if (!targetFile.getParentFile().exists())
      {
        targetFile.getParentFile().mkdir();
      }
      outputStream = new FileOutputStream(targetFile);
      //FileCopyUtils springboot的方法
      FileCopyUtils.copy(inputStream, outputStream);

      //File uploadFile = new File(path + fileName);
      //可以上传File 或者路径
      putObjectResult = CosUtil.putCosVideoObject(path + fileName);
      if(putObjectResult == null){
        return "视频文件上传到COS失败";
      }

      String imageName = fileShortName + ".jpg";
      // COS端生成图片
      CosUtil.getSnapshotPic(fileName,imageName);

      //保存数据到数据库
      VideoListEntity videoListEntity = new VideoListEntity();
      videoListEntity.setAuthor("黄嘉鑫");
      videoListEntity.setCategoryId(1);
      videoListEntity.setCollectNum(0);
      videoListEntity.setCommentNum(0);
      videoListEntity.setLikeNum(0);
      videoListEntity.setCoverurl(CosConstant.Image_Url_Prefix + imageName);   //截图的URL
      videoListEntity.setHeadurl(CosConstant.MyHeaderImage);
      videoListEntity.setPlayurl(CosConstant.Video_Url_Prefix + fileName);  // 视频的URL
      videoListEntity.setVtitle("");  //标题
      videoListService.save(videoListEntity);


      return "上传成功";
    }
    catch (IOException e)
    {
      e.printStackTrace();
      return "上传失败";
    }
    //关闭流
    finally {
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (outputStream != null)
        try {
          outputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
    }
  }
}